package com.mew.game.slots;

import com.mew.common.rest.game.SlotsEnterRoomRequest;
import com.mew.common.rest.game.SlotsEnterRoomResponse;
import com.mew.common.rest.sas.EnterGameSasRequest;
import com.mew.common.rest.sas.ExitGameSasRequest;
import com.zebra.common.event.EventBusAsyncCenter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@Slf4j
public class SlotsEnterExitRoomController {

    @Autowired
    private SlotsEnterRoomService slotsEnterExitRoomService;

    @PostMapping(value = "/slots/enter")
    @ResponseBody
    public SlotsEnterRoomResponse enterRoom(@RequestBody SlotsEnterRoomRequest request){
        SlotsEnterRoomResponse slotsEnterRoomResponse = null;
        try {
            log.info("Slots Enter: {}", request.toString());
            slotsEnterRoomResponse = slotsEnterExitRoomService.enterRoom(request);
            EventBusAsyncCenter.post(EnterGameSasRequest.builder().userId(request.getUid()).roomId(request.getRoomId()).gameId(request.getGameId()).build());
            return slotsEnterRoomResponse;
        }catch (Exception e){
            if (null != slotsEnterRoomResponse){
                log.error("Slots Enter event error,  uid:"+request.getUid(), e);
                return slotsEnterRoomResponse;
            }
            log.error("Slots Enter uid:"+request.getUid(), e);
            return SlotsEnterRoomResponse.builder().errorState(500).build();
        }
    }


    @GetMapping(value = "/slots/exit/{uid}/{exittype}")
    @ResponseBody
    public Integer exitRoom(@PathVariable("uid") long uid, @PathVariable("exittype") int exittype){
        log.info("Slots ExitRoom u: {}  ", uid);
        try {
            slotsEnterExitRoomService.exitGame(uid, exittype);
            return 1;
        }catch (Exception e){
            log.error("Slots ExitRoom u "+uid, e);
            return  0;
        }
    }



}
